package cxt.com.recyclerview.demo1;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
 * 作者：chengxiangtong on 2017/1/9 16:22
 * 邮箱：528440900@qq.com
 */

public class NormalAdapterWrapper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private NormalAdapter mNormalAdapter;
    private View mHeader;
    private View mFooter;

    public NormalAdapterWrapper(NormalAdapter mNormalAdapter) {
        this.mNormalAdapter = mNormalAdapter;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE.HEADER.ordinal()) {
            return new RecyclerView.ViewHolder(mHeader) {
            };
        } else if (viewType == ITEM_TYPE.FOOTER.ordinal()) {
            return new RecyclerView.ViewHolder(mFooter) {
            };
        } else {
            return mNormalAdapter.onCreateViewHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            return;
        } else if (position == mNormalAdapter.getItemCount() + 1) {
            return;
        } else {
            mNormalAdapter.onBindViewHolder(((NormalAdapter.ViewHolders) holder), position - 1);
        }
    }

    @Override
    public int getItemCount() {
        return mNormalAdapter.getItemCount() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return ITEM_TYPE.HEADER.ordinal();
        } else if(position == mNormalAdapter.getItemCount() + 1){
            return ITEM_TYPE.FOOTER.ordinal();
        } else{
            return ITEM_TYPE.NORMAL.ordinal();
        }
    }

    public void addHeaderView(View view) {
        this.mHeader = view;
    }

    public void addFooterView(View view) {
        this.mFooter = view;
    }

     enum ITEM_TYPE {
        HEADER,
        FOOTER,
        NORMAL
    }
}
